package com.idol.service.impl;

import com.idol.dao.WebTokenMapper;
import com.idol.entity.WebToken;
import com.idol.service.ITockenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author SupremeSir
 * @description
 * @className TockenServiceImpl
 * @date 2021/2/16 0:55
 **/
@Service
public class TockenServiceImpl implements ITockenService {
    @Autowired
    private WebTokenMapper webTokenMapper;

    @Override
    public boolean saveTocken(String email, String tocken) {
        WebToken webToken = new WebToken();
        webToken.setEmail(email);
        webToken.setToken(tocken);
        return webTokenMapper.insert(webToken) > 0 ? true : false;
    }

    @Override
    public String getInfo(String token) {
        Example example = new Example(WebToken.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("token", token);
        example.orderBy("id").desc();
        List<WebToken> webTokens = webTokenMapper.selectByExample(example);
        if (webTokens.isEmpty()) {
            return null;
        }
        return webTokens.get(0).getEmail();
    }
}
